package com.cmict.data.infra.service.consultcategory;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import com.cmict.data.infra.controller.admin.consultcategory.vo.*;
import com.cmict.data.infra.dal.dataobject.consultcategory.ConsultCategoryDO;
import com.cmict.data.framework.common.pojo.PageResult;

import com.cmict.data.infra.convert.consultcategory.ConsultCategoryConvert;
import com.cmict.data.infra.dal.mysql.consultcategory.ConsultCategoryMapper;

import static com.cmict.data.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cmict.data.infra.enums.ErrorCodeConstants.*;

/**
 * 问答分类 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ConsultCategoryServiceImpl implements ConsultCategoryService {

    @Resource
    private ConsultCategoryMapper consultCategoryMapper;

    @Override
    public Long createConsultCategory(ConsultCategoryCreateReqVO createReqVO) {
        // 插入
        ConsultCategoryDO consultCategory = ConsultCategoryConvert.INSTANCE.convert(createReqVO);
        ConsultCategoryDO consultCategorySelect = consultCategoryMapper.selectOne(consultCategory.getCategoryOrder());
        if(null != consultCategorySelect){
            consultCategoryMapper.updateCategoryOrder(consultCategory.getCategoryOrder());
        }
        consultCategoryMapper.insert(consultCategory);
        // 返回
        return consultCategory.getId();
    }

    @Override
    public void updateConsultCategory(ConsultCategoryUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateConsultCategoryExists(updateReqVO.getId());
        // 更新
        ConsultCategoryDO updateObj = ConsultCategoryConvert.INSTANCE.convert(updateReqVO);
        updateObj.setCreateTime(LocalDateTime.now());
        if(null != updateObj && null == updateObj.getCategoryOrder()){
            consultCategoryMapper.updateById(updateObj);
        }else if(null != updateObj && null != updateObj.getCategoryOrder()){
            ConsultCategoryDO beiConsultCategory = consultCategoryMapper.selectOne(updateObj.getCategoryOrder());
            if(null != beiConsultCategory && updateObj.getId() != beiConsultCategory.getId()){
                ConsultCategoryDO yuanConsultCategory = consultCategoryMapper.selectById(updateObj.getId());
                Integer yuanCategoryOrder = yuanConsultCategory.getCategoryOrder();
                Integer beiCategoryOrder = beiConsultCategory.getCategoryOrder();
                if(yuanCategoryOrder < beiCategoryOrder){
                    consultCategoryMapper.littleToBig(yuanCategoryOrder,beiCategoryOrder);
                }else if(yuanCategoryOrder > beiCategoryOrder){
                    consultCategoryMapper.bigToLittle(yuanCategoryOrder,beiCategoryOrder);
                }
                consultCategoryMapper.updateById(updateObj);
            }else{
                consultCategoryMapper.updateById(updateObj);
            }
        }

    }

    @Override
    public void deleteConsultCategory(Long id) {
        // 校验存在
        this.validateConsultCategoryExists(id);
        // 删除
        consultCategoryMapper.deleteById(id);
    }

    private void validateConsultCategoryExists(Long id) {
        if (consultCategoryMapper.selectById(id) == null) {
            throw exception(CONSULT_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public ConsultCategoryDO getConsultCategory(Long id) {
        return consultCategoryMapper.selectById(id);
    }

    @Override
    public List<ConsultCategoryDO> getConsultCategoryList(ConsultCategoryBaseVO baseVO) {
        return consultCategoryMapper.getConsultCategoryList(baseVO);
    }

    @Override
    public PageResult<ConsultCategoryDO> getConsultCategoryPage(ConsultCategoryPageReqVO pageReqVO) {
        return consultCategoryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ConsultCategoryDO> getConsultCategoryList(ConsultCategoryExportReqVO exportReqVO) {
        return consultCategoryMapper.selectList(exportReqVO);
    }

}
